package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import cn.com.kh.signUtil
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 工单查询API
 */

public class apiGetWODetail implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiGetWODetail.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        JSONObject result = new JSONObject();

        JSONObject resultJson = null;
        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("[调用请求信息]:" + callJson);

        JSONObject signJson = signUtil.CheckSign(JSONObject.parseObject(callJson));
        log.info("[签名验证结果]:");
        log.info(signJson);
        if (signJson != null) {
            return signJson.toString();
        } else {
            try {
                //解析Json
                JSONObject inJson = JSONObject.parseObject(callJson);

                //获取查询参数
                String s_workorder_guid = inJson.getJSONObject("data").getString("s_workorder_guid");// 输入的工单ID

                log.debug("请求参数-查询条件-s_workorder_guid:" + s_workorder_guid);

                String q_WhereSQL = " Where Fm.Category <> 'SALE01' And Eta.Obflag = 0";
                log.debug("初始化控制查询条件SQL:" + q_WhereSQL);

                // 模糊查询条件处理
                if (!KHUtil.isNull(s_workorder_guid)) {
                    q_WhereSQL = q_WhereSQL + " And Eta.objective_guid = '" + s_workorder_guid + "'";
                }

                q_WhereSQL = q_WhereSQL + "Limit 1";
                log.debug("控制查询条件完成SQL:" + q_WhereSQL);

                String queryWorkOrderSQL = "Select Distinct Obj.Workorderid As workorder_id,\n" +
                        "                Obj.Objective_Guid As objective_guid,\n" +
                        "                Eta.Elitetask_Guid As elitetask_guid,\n" +
                        "                Fm.Mapname As map_name,\n" +
                        "                Nm.Nodemapname As node_name,\n" +
                        "                Cus.Customername As customer_name,\n" +
                        "                Cus.Mobile As mobile,\n" +
                        "                Date_Format(Eta.Expectstarttime, '%Y-%m-%d %H:%i:%S') As expect_starttime,\n" +
                        "                Date_Format(Eta.Expectendtime, '%Y-%m-%d %H:%i:%S') As expect_endtime,\n" +
                        "                Priy.Prioritydescription As priority,\n" +
                        "                R1.Rolegroupdescription As rolegroup_name,\n" +
                        "                S1.Staffname As staff_name,\n" +
                        "                Eta.Comments As comments,\n" +
                        "				 Tbi.workorder_id as 'workorder_id',\n" +
                        "				 Tbi.workorder_type as 'workorder_type',\n" +
                        "				 Tbi.acceptor as 'accept_acceptor',\n" +
                        "				 Tbi.writedate as 'accept_writedate',\n" +
                        "				 Tbi.undertaker as 'undertake_undertaker',\n" +
                        "				 Tbi.undertaker_mobile as 'undertaker_mobile',\n" +
                        "				 Tbi.undertake_identity as 'undertake_identity',\n" +
                        "				 Tbi.undertake_hometown as 'undertake_hometown',\n" +
                        "				 Tbi.undertake_address as 'undertake_address',\n" +
                        "				 Tbi.tenant as 'bid_tenant',\n" +
                        "				 Tbi.tenant_mobile as 'bid_tenant_mobile',\n" +
                        "				 Tbi.tenant_code as 'bid_tenant_code',\n" +
                        "				 Tbi.tenant_hometown as 'bid_tenant_hometown',\n" +
                        "				 Tbi.tenant_address as 'bid_tenant_address',\n" +
                        "				 Tbi.carno as 'vehicle_carno',\n" +
                        "				 Tbi.driver_license_name as 'vehicle_driver_license_name',\n" +
                        "				 Tbi.driver_license_number as 'vehicle_driver_license_number',\n" +
                        "				 Tbi.driving_license_name as 'vehicle_driving_license_name',\n" +
                        "				 Tbi.driving_license_number as 'vehicle_driving_license_number',\n" +
                        "				 Tbi.vehicle_model as 'vehicle_model',\n" +
                        "				 Tbi.old_tenant as 'sublet_old_tenant',\n" +
                        "				 Tbi.old_contract as 'sublet_old_contract',\n" +
                        "				 Tbi.retreat_deposit_reason as 'unsublet_retreat_deposit_reason',\n" +
                        "				 Tbi.builder as 'fitment_builder',\n" +
                        "				 Tbi.identity_number as 'fitment_identity_number',\n" +
                        "				 Tbi.valid_documents as 'fitment_valid_documents',\n" +
                        "				 Tbi.construction_time as 'fitment_construction_time',\n" +
                        "				 Tbi.construction_unit as 'fitment_construction_unit',\n" +
                        "				 Tbi.fire as 'fitment_fire',\n" +
                        "				 Tbi.contract_title as 'contract_title',\n" +
                        "				 Tbi.lease_term as 'contract_lease_term',\n" +
                        "				 Tbi.stalls_code as 'contract_stalls_code',\n" +
                        "				 Tbi.area as 'contract_area',\n" +
                        "				 Tbi.operating_area as 'contract_operating_area',\n" +
                        "				 Tbi.business_type as 'contract_business_type',\n" +
                        "				 Tbi.monthly_rent as 'coast_monthly_rent',\n" +
                        "				 Tbi.monthly_management_fee as 'coast_monthly_management_fee',\n" +
                        "				 Tbi.lease_deposit as 'coast_lease_deposit',\n" +
                        "				 Tbi.water_electricity_deposit as 'coast_water_electricity_deposit',\n" +
                        "				 Tbi.decoration_deposit as 'coast_decoration_deposit',\n" +
                        "				 Tbi.earnest_money as 'coast_earnest_money',\n" +
                        "				 Tbi.renamed_money as 'coast_renamed_money',\n" +
                        "				 Tbi.deposit_summary as 'coast_deposit_summary',\n" +
                        "				 Tbi.deposit_amount as 'coast_deposit_amount',\n" +
                        "				 Tbi.water_fee as 'coast_water_fee',\n" +
                        "				 Tbi.electricity_bill as 'coast_electricity_bill',\n" +
                        "				 Tbi.electricity_service_charge as 'coast_electricity_service_charge',\n" +
                        "				 Tbi.summary_payment as 'coast_summary_payment',\n" +
                        "				 Tbi.toll_standard as 'coast_toll_standard',\n" +
                        "				 Tbi.handling_department as 'batchbid_handling_department',\n" +
                        "				 Tbi.batch_operating_area as 'batchbid_operating_area',\n" +
                        "				 Tbi.stalls_subtotal as 'batchbid_stalls_subtotal',\n" +
                        "				 Tbi.contract_content as 'batchbid_contract_content',\n" +
                        "				 Tbi.retreat_deposit_type as 'categorylable_retreat_deposit_type',\n" +
                        "				 Tbi.inside_outside as 'categorylable_inside_outside',\n" +
                        "				 Tbi.stalls_orarea_decoration as 'categorylable_stalls_orarea_decoration'\n"+
                        "  From Elitetask Eta\n" +
                        "  Left Join Objective Obj\n" +
                        "    On Eta.Objective_Guid = Obj.Objective_Guid\n" +
                        "  Left Join Tabcwo_business_info Tbi\n" +
                        "    On Tbi.relation_id = Obj.Objective_Guid\n" +
                        "  Left Join Wo3_Frame_Map Fm\n" +
                        "    On Fm.Map_Id = Eta.Map_Id\n" +
                        "  Left Join Wo3_Node_Map Nm\n" +
                        "    On Eta.Curstep_Id_Map = Nm.Nodemap_Id\n" +
                        "   And Nm.Map_Id = Fm.Map_Id\n" +
                        "  Left Join Customer Cus\n" +
                        "    On Eta.Customer_Guid = Cus.Customer_Guid\n" +
                        "  Left Join Sysc_Priority Priy\n" +
                        "    On Eta.Priority = Priy.Priority_Id\n" +
                        "  Left Join Rolegroup R1\n" +
                        "    On R1.Rolegroup_Id = Eta.Handlegroup_Id\n" +
                        "  Left Join Staff S1\n" +
                        "    On S1.Staff_Id = Eta.Handleby_Id\n" +
                        "  Left Join Staff S2\n" +
                        "    On S2.Staff_Id = Eta.Modifiedby\n" +
                        "  Left Join Staffrolegroup Sr\n" +
                        "    On Sr.Staff_Id = S1.Staff_Id" + q_WhereSQL;

                log.debug("执行工单查询数据SQL:" + queryWorkOrderSQL);

                List WorkOrderList = eliteBean.
                //获取总行数
                log.debug("执行工单查询数据SQL获取总条数:" + WorkOrderList.size());

                String reJoson = Util.getJsonFromList(WorkOrderList);
                reJoson=!KHUtil.isNull(reJoson)?reJoson:"[]";
                long endTime = System.currentTimeMillis();
                result.put("code", 1);
                result.put("msg", "处理完成、调用问题处理时长(毫秒):" + (endTime - startTime));
                JSONArray dataArr = JSONArray.parseArray(reJoson);
                result.put("data", dataArr);

            } catch (Exception e) {
                result.put("code", 1008);
                result.put("msg", e.getMessage());
            }

        }

        return result.toString();
    }
}